import pickle
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class SeleniumUtils:
    @staticmethod
    def load_cookie(driver, domain, cookie_path):
        # 再加载cookie
        cookies = pickle.load(open(cookie_path, "rb"))
        for cookie in cookies:
            print(cookie)
            cookie_dict = {
                'domain': domain,
                'name': cookie.get('name'),
                'value': cookie.get('value')
            }
            driver.add_cookie(cookie_dict)
        print("load_cookie: {}".format(cookie_path))

    @staticmethod
    def save_cookie(driver, cookie_path):
        pickle.dump(driver.get_cookies(), open(cookie_path, 'wb'))
        print("save_cookie: {}".format(cookie_path))

    # 判断是否切换了新的窗口，并判断元素是否可见
    @staticmethod
    def watting_new_window_visibility(driver, by_type, ele_value, timeout):
        time.sleep(1)
        new_window = driver.window_handles[-1]  # 找到新窗口名字
        driver.switch_to.window(new_window)  # 切换到最新打开的窗口
        ret = WebDriverWait(driver, timeout, 1).until(EC.visibility_of_element_located(
            (by_type, ele_value)))
        return ret

    @staticmethod
    def watting_visibility(driver, by_type, ele_value, timeout=15):
        ret = WebDriverWait(driver, timeout, 1).until(EC.visibility_of_element_located(
            (by_type, ele_value)))
        return ret

    @staticmethod
    def watting_invisibility(driver, by_type, ele_value, timeout=15):
        ret = WebDriverWait(driver, timeout, 1).until(EC.invisibility_of_element_located(
            (by_type, ele_value)))
        return ret

    @staticmethod
    def watting_existed(driver, by_type, ele_value, timeout=15):
        ret = WebDriverWait(driver, timeout, 1).until(EC.presence_of_element_located(
            (by_type, ele_value)))
        return ret

    # 判断网页是否加载完成
    @staticmethod
    def is_loaded(driver):
        ret = (driver.execute_script("return document.readyState") == "complete")
        return ret

    @staticmethod
    def alert_accept(driver, timeout=15):
        wait = WebDriverWait(driver, timeout)
        wait.until(EC.alert_is_present())
        driver.switch_to.alert.accept()

    @staticmethod
    def alert_dismiss(driver, timeout=15):
        wait = WebDriverWait(driver, timeout)
        wait.until(EC.alert_is_present())
        driver.switch_to.alert.dismiss()

    # 滚动到底部
    @staticmethod
    def scroll_bottom(driver):
        # js = "var q=document.documentElement.scrollTop=10000"
        # js_down = "window.scrollTo(0,10000)"

        # 动态执行滑倒底部 document.body.scrollHeight
        js_down = "window.scrollTo(0,document.body.scrollHeight)"
        driver.execute_script(js_down)

    # 移动到元素上然后点击
    @staticmethod
    def move_click(driver, ele, offset=5):
        ActionChains(driver).move_to_element(ele).move_by_offset(offset, offset).click().perform()

    # 移动到元素上然后点击
    @staticmethod
    def move_click_by(driver, by_type, ele_value, offset=5):
        ele = driver.find_element(by_type,
                                  ele_value)
        ActionChains(driver).move_to_element(ele).move_by_offset(offset, offset).click().perform()
